import i18n from "i18next"
import { initReactI18next } from "react-i18next"
import en from "./en.json"
import zh_ch from "./zh_ch.json"

const resources = {
    en: {
        translation: en
    },
    zh_ch: {
        translation: zh_ch
    }
}

const initLng = localStorage.getItem("language") || "zh_ch"

// 初始化语言
i18n.use(initReactI18next).init({
    resources,
    lng: initLng,
    fallbackLng: "zh_ch",
    interpolation: {
        escapeValue: false
    }
})

// 切换语言
export const changeLng = (lng: string = "zh_ch") => {
    i18n.changeLanguage(lng)
        .then(() => {
            localStorage.setItem("language", lng)
        })
        .catch(() => {
            console.error("切换语言失败")
        })
}
